<template>
  <a-modal
    :title="title"
    :width="520"
    :visible="visible"
    :maskClosable="false"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-form :form="form">
      <a-form-item
        style="display: none;"
        :labelCol="labelCol"
        :wrapperCol="wrapperCol"
      >
        <a-input v-decorator="['id']"/>
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol">
        <span slot="label">
          <a-tooltip
            title="只能设置一个"
          >
            弹窗提醒<a-icon type="question-circle-o" style="margin-left: 5px"/></a-tooltip
          >
        </span>
        <a-switch
          id="show"
          checkedChildren="开启"
          unCheckedChildren="关闭"
          v-decorator="['is_stress']"
        />
      </a-form-item>
      <a-form-item
        label="公告标题"
        :labelCol="labelCol"
        :wrapperCol="wrapperCol"
        has-feedback
      >
        <a-input placeholder="请输入公告标题" v-decorator="['title', {rules: [{required: true, message: '请输入公告标题！'}]}]"/>
      </a-form-item>
      <a-form-item
        label="公告内容"
        :labelCol="labelCol"
        :wrapperCol="wrapperCol"
        has-feedback
      >
        <a-textarea placeholder="请输入公告内容" :rows="4"
                    v-decorator="['content', {rules: [{required: true, message: '请输入公告内容！'}]}]"/>
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script>
  import * as api from '@/api/system/noticeManage'
  // 导入提示信息字典
  import config from '@/config/dict'

  export default {
    name: 'sForm',
    data() {
      return {
        labelCol: {
          xs: { span: 10 },
          sm: { span: 5 },
          md: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 },
          md: { span: 18 }
        },
        visible: false,
        title: '',
        roles: [],
        pwdShow: true,
        passwordRequired: true,
        form: this.$form.createForm(this)
      }
    },
    methods: {
      // 初始化方法
      open(record) {
        this.visible = true
        if (record) {
          this.title = '编辑公告'
        } else {
          this.title = '新增公告'
        }

        if (record) {
          setTimeout(() => {
            this.setUserItem(record)
          }, 100)
        }
      },
      setUserItem(record) {
        this.form.setFieldsValue(
          {
            id: record.id,
            title: record.title,
            content: record.content,
            is_stress: record.is_stress
          }
        )
      },
      // [确认按钮]提交数据方法
      handleSubmit() {
        const { form: { validateFields } } = this
        validateFields((errors, values) => {
          if (!errors) {
            if (values.id) {
              this.message = config.msgDict[1]
              api.updateObj(values).then((res) => {
                this._handle_response(res, values)
              })
            } else {
              this.message = config.msgDict[0]
              api.addObj(values).then((res) => {
                this._handle_response(res, values)
              })
            }
          }
        })
      },
      // 处理返回消息
      _handle_response(res, values) {
        if (!res.error) {
          this.$message.success(this.message['success'])
          this.handleCancel()
          this.$emit('ok', values)
        } else {
          this.$message.error(this.message['failed'] + res.error)
        }
      },
      // [取消按钮]重置数据,关闭弹框
      handleCancel() {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
